﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Security.Policy;
using System.Threading;
using System.Timers;
using System.Net;
using Mighty.PlayingBoard;
using Mighty.Brain;


namespace Mighty.DialogPlayingBoard
{
    public partial class Main : Form, IDebug
    {
        public MightyModule mightyMoidule = new MightyModule();
        public Hashtable moduleHash = new Hashtable();
        public PlayingBoard.PlayingBoard playingBoard = null;

        public String gameMsg;
        public String summaryMsg;
        public int[] credit = new int[5];
        public int gameCount = 0;
        public String[] selectModuleList = new String[5];

        Thread gameThread = null;

        public Main()
        {
            InitializeComponent();

            credit[0] = (int)updownCredit0.Value;
            credit[1] = (int)updownCredit1.Value;
            credit[2] = (int)updownCredit2.Value;
            credit[3] = (int)updownCredit3.Value;
            credit[4] = (int)updownCredit4.Value;

            //DownloadUpdatedDLL();
            LoadDLL();

            System.Timers.Timer threadTimer = new System.Timers.Timer(500);
            threadTimer.Elapsed += new ElapsedEventHandler(RequestTimer);
            threadTimer.AutoReset = true;
            threadTimer.Start();

            playingBoard = new PlayingBoard.PlayingBoard();
            playingBoard.iDebug = this;

            txtCurDirectory.Text = Directory.GetCurrentDirectory();
        }

        private void RequestTimer(object source, ElapsedEventArgs e)
        {
            RefreshGameMsg();
            RefreshSummaryMsg();
            RefreshGameButton();
            RefreshGameCount();
            RefreshCredit0();
            RefreshCredit1();
            RefreshCredit2();
            RefreshCredit3();
            RefreshCredit4();
        }


        private void RefreshGameMsg()
        {
            if (txtGameMsg.InvokeRequired) Invoke(new MethodInvoker(RefreshGameMsg));
            else txtGameMsg.Text = gameMsg;
        }

        private void RefreshSummaryMsg()
        {
            if ( txtSummaryMsg.InvokeRequired) Invoke(new MethodInvoker(RefreshSummaryMsg));
            else txtSummaryMsg.Text = summaryMsg;
        }

        private void RefreshGameButton()
        {
            if (btnRun.InvokeRequired)
            {
                Invoke(new MethodInvoker(RefreshGameButton));
            }
            else
            {
                if (gameThread != null)
                {
                    btnRun.Text = "게임중지";
                }
                else
                {
                    btnRun.Text = "게임시작";
                }
            }
        }

        private void RefreshGameCount()
        {
            if (lblGameCount.InvokeRequired) Invoke(new MethodInvoker(RefreshGameCount));
            else lblGameCount.Text = gameCount.ToString();
        }

        private void RefreshCredit0()
        {
            if (updownCredit0.InvokeRequired) Invoke(new MethodInvoker(RefreshCredit0));
            else updownCredit0.Value = credit[0];
        }

        private void RefreshCredit1()
        {
            if (updownCredit1.InvokeRequired) Invoke(new MethodInvoker(RefreshCredit1));
            else updownCredit1.Value = credit[1];
        }

        private void RefreshCredit2()
        {
            if (updownCredit2.InvokeRequired) Invoke(new MethodInvoker(RefreshCredit2));
            else updownCredit2.Value = credit[2];
        }

        private void RefreshCredit3()
        {
            if (updownCredit3.InvokeRequired) Invoke(new MethodInvoker(RefreshCredit3));
            else updownCredit3.Value = credit[3];
        }

        private void RefreshCredit4()
        {
            if (updownCredit4.InvokeRequired) Invoke(new MethodInvoker(RefreshCredit4));
            else updownCredit4.Value = credit[4];
        }


        public void LoadDLL()
        {
            mightyMoidule.LoadDLL();

            try
            {
                // combobox 설정
                comboModule0.Items.Clear();
                comboModule1.Items.Clear();
                comboModule2.Items.Clear();
                comboModule3.Items.Clear();
                comboModule4.Items.Clear();

                foreach (ModuleInfo module in mightyMoidule.moduleHash.Values)
                {
                    comboModule0.Items.Add(module.moduleID);
                    comboModule1.Items.Add(module.moduleID);
                    comboModule2.Items.Add(module.moduleID);
                    comboModule3.Items.Add(module.moduleID);
                    comboModule4.Items.Add(module.moduleID);
                }

                comboModule0.Text = "Brain.BasicBrain";
                comboModule1.Text = "Brain.BasicBrain";
                comboModule2.Text = "Brain.BasicBrain";
                comboModule3.Text = "Brain.BasicBrain";
                comboModule4.Text = "Brain.BasicBrain";

                comboModule0.Select();

                if (File.Exists("ai.txt"))
                {

                    StreamReader sr = new StreamReader("ai.txt");
                    List<String> lines = new List<String>();
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Trim() == "") continue;
                        if (line[0] == '#') continue;
                        lines.Add(line);
                    }
                    if (lines.Count > 0) comboModule0.Text = lines[0];
                    if (lines.Count > 1) comboModule1.Text = lines[1];
                    if (lines.Count > 2) comboModule2.Text = lines[2];
                    if (lines.Count > 3) comboModule3.Text = lines[3];
                    if (lines.Count > 4) comboModule4.Text = lines[4];

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void SetModuleMsg(int position, String msg)
        {
            if (position == 0) txtModuleMsg0.Text = msg;
            else if (position == 1) txtModuleMsg1.Text = msg;
            else if (position == 2) txtModuleMsg2.Text = msg;
            else if (position == 3) txtModuleMsg3.Text = msg;
            else if (position == 4) txtModuleMsg4.Text = msg;
        }

        public void DebugMsgClear()
        {
            gameMsg += "\r\n\r\n";
        }

        public void DebugMsgAdd(String msg)
        {
            gameMsg += "\r\n" + msg;
        }

        public void SummaryMsgClear()
        {
            summaryMsg += "\r\n\r\n";
        }

        public void SummaryMsgAdd(String msg)
        {
            summaryMsg += "\r\n" + msg;
        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            if (gameThread != null)
            {
                gameThread.Abort();
                gameThread = null;
            }
            else
            {
                gameMsg = "";
                summaryMsg = "";

                selectModuleList[0] = comboModule0.Text;
                selectModuleList[1] = comboModule1.Text;
                selectModuleList[2] = comboModule2.Text;
                selectModuleList[3] = comboModule3.Text;
                selectModuleList[4] = comboModule4.Text;

                if (chkTimeout.Checked) playingBoard.SetTimeOutLimit(100);
                else playingBoard.SetTimeOutLimit(0);

                gameThread = new Thread(RequestThread);
                gameThread.Start();
            }
        }

        private void RequestThread()
        {

            for (int i = 0; i < updownRunCount.Value; i++)
            {
                gameCount++;

                InitGame();
                playingBoard.StartGame();
                SaveGame();
            }
            gameThread = null;
        }

        public void InitGame()
        {
            try
            {
                ModuleInfo m;
                for (int i = 0; i < 5; i++)
                {
                    String selectModule = selectModuleList[i];
                    m = (ModuleInfo)mightyMoidule.moduleHash[selectModule];

                    playingBoard.SetAI(m.moduleName, i, m.moduleType);
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public void SaveGame()
        {
            int[] playerScore = playingBoard.GetScore();

            credit[0] += playerScore[0];
            credit[1] += playerScore[1];
            credit[2] += playerScore[2];
            credit[3] += playerScore[3];
            credit[4] += playerScore[4];
        }



        private void OnSelectedValueChanged1(object sender, EventArgs e)
        {
            if (comboModule0.Text == "") return;
            ModuleInfo m = (ModuleInfo)mightyMoidule.moduleHash[comboModule0.Text];
            SetModuleMsg(0, m.dllNote);
        }

        private void OnSelectedValueChanged2(object sender, EventArgs e)
        {
            if (comboModule1.Text == "") return;
            ModuleInfo m = (ModuleInfo)mightyMoidule.moduleHash[comboModule1.Text];
            SetModuleMsg(1, m.dllNote);
        }

        private void OnSelectedValueChanged3(object sender, EventArgs e)
        {
            if (comboModule2.Text == "") return;
            ModuleInfo m = (ModuleInfo)mightyMoidule.moduleHash[comboModule2.Text];
            SetModuleMsg(2, m.dllNote);
        }

        private void OnSelectedValueChanged4(object sender, EventArgs e)
        {
            if (comboModule3.Text == "") return;
            ModuleInfo m = (ModuleInfo)mightyMoidule.moduleHash[comboModule3.Text];
            SetModuleMsg(3, m.dllNote);
        }

        private void OnSelectedValueChanged5(object sender, EventArgs e)
        {
            if (comboModule4.Text == "") return;
            ModuleInfo m = (ModuleInfo)mightyMoidule.moduleHash[comboModule4.Text];
            SetModuleMsg(4, m.dllNote);
        }

        #region IDebug 멤버

        public void ClearMessage()
        {
            DebugMsgClear();
        }

        public void ClearSummary()
        {
            SummaryMsgClear();
        }

        public void AddMessage(string msg)
        {
            DebugMsgAdd(msg);
        }

        public void AddSummary(string msg)
        {
            SummaryMsgAdd(msg);
        }

        #endregion
    }

}